package com.ruoyi.project.module.lys;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;

import com.ruoyi.project.domain.CxcyTransformation;
import com.ruoyi.project.tool.HttpUtils;
import com.ruoyi.project.tool.WeChatAccessTokenUtils;
import com.ruoyi.project.tool.WeChatUtils;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class LysCxcyTransformationWeChatAuditMassage {

    // 修改这里：方法参数可以更加内容进行修改，userid是必备的
    //public static void sendWeChatMassage(String userId,String username,String result) throws JSONException {
    public static void sendWeChatAuditMassage(CxcyTransformation cxcyTransformation) throws JSONException {

        //时间标准化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String timeS = simpleDateFormat.format(cxcyTransformation.getApplyTime());

        //基础企业信息和secret
        String corpid = "ww3c8d31017de5aa90";
        String corpsecret = "u7TzxLgg1AEEhutWpJEzxV1l9NHNq-Cr1PF7y1snuXc";//设备预约系统，已经修改好

        // 获取access_token json字符串
        String accessToken = WeChatAccessTokenUtils.getAccessToken(corpid, corpsecret);
        // 发送应用消息
        String userid_url = WeChatUtils.QY_WEIXIN_MESSAGE_PUSH
                .replace("ACCESS_TOKEN", accessToken);

        JSONObject obj1 = new JSONObject();

        // 修改这里：可以根据userid查询用户其他信息，例如用姓名等 待完善

        // 修改这里：消息内容自己拼接

        // 接口文档地址：https://developer.work.weixin.qq.com/document/path/90236
        if (cxcyTransformation.getAuditState().equals("2")) {
            obj1.put("content", "【创新创业成果转化信息】\n" +
                    cxcyTransformation.getApplyName() + " (" + cxcyTransformation.getApplyNumber() + ") " + "你好，你的成果转化申请已通过审核！\n\n" +
                    "成果名称：" + cxcyTransformation.getResultName() + "\n" +
                    "完成单位：" + cxcyTransformation.getCompletionUnit() + "\n" +
                    "申请时间：" + timeS + "\n" +
                    "负责人姓名：" + cxcyTransformation.getFirstName() + "\n" +
                    "负责人学号：" + cxcyTransformation.getFirstNumber() + "\n\n" +
                    "请前往系统查看。");
        }else if (cxcyTransformation.getAuditState().equals("3")) {
            obj1.put("content", "【创新创业成果转化信息】\n" +
                    cxcyTransformation.getApplyName() + " (" + cxcyTransformation.getApplyNumber() + ") " + "你好，你的成果转化申请未通过审核！\n\n" +
                    "成果名称：" + cxcyTransformation.getResultName() + "\n" +
                    "完成单位：" + cxcyTransformation.getCompletionUnit() + "\n" +
                    "申请时间：" + timeS + "\n" +
                    "负责人姓名：" + cxcyTransformation.getFirstName() + "\n" +
                    "负责人学号：" + cxcyTransformation.getFirstNumber() + "\n\n" +
                    "请前往系统查看。");
        }

        JSONObject obj = new JSONObject();
        obj.put("touser",cxcyTransformation.getApplyNumber());
        obj.put("msgtype", "text");
        obj.put("agentid", "1000202");//设备预约系统，已经修改好
        obj.put("text", obj1);

        //System.out.println(obj.toString()+"1111");
        // 执行消息推送
        HttpUtils.sendPost(userid_url, obj.toString());

    }

}
